//
//  ChangeThemeViewController.swift
//  XMenSwift
//
//  Created by Jason on 2023/8/10.
//

import UIKit
import SwiftTheme


class ChangeThemeViewController: UIViewController {
    
    @IBOutlet weak var myLabel: UILabel!
    
    @IBOutlet weak var myButton: UIButton!
    
    @IBOutlet weak var myImageView: UIImageView!
    
    var themeArray: [ThemeType] = [.default, .other, .default, .other]
    
    var currentIndex = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        myLabel.setThemeTextColors()
        myLabel.setThemeTextFont(size: 20)
        myButton.setThemeTextColors(forState: .normal)
        myButton.setThemeImageNames(["icon_match","icon_nav_add","icon_arrow_left","icon_scan"], forState: .normal)
        myButton.setThemeTextFont(weight: .bold, size: 20)
        myImageView.setThemeImageNames(["1_t","2_t","1","2"])
    }

    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        guard
            #available(iOS 13.0, *),
            traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection)
            else { return }
        updateTheme()
    }

    private func updateTheme() {
        guard #available(iOS 12.0, *) else { return }
        
        switch traitCollection.userInterfaceStyle {
        case .light:
            APPThemeChangeManage.setTheme(index: 1)
            break
        case .dark:
            APPThemeChangeManage.setTheme(index: 3)
            break
        case .unspecified:
            break
        @unknown default:
            break
        }
    }

    @IBAction func changeTheme(_ sender: Any) {
        currentIndex += 1
        if self.currentIndex < themeArray.count {
        } else {
            currentIndex = 0
        }
        changeThemeForIndex(currentIndex)
    }
    
    func changeThemeForIndex(_ index: Int) {
        let theme = themeArray[currentIndex]
        APPThemeChangeManage.setTheme(index: theme.rawValue)
    }
}
